<template>
  <div>
    <div class="top-banner-back"><el-button type="primary" @click="toEdit(0)">新增校区</el-button></div>
    <div class="edu-main" v-if="findAllSchoolList.length != 0">
        <div class="edu-main-title">已创建的校区</div>
        <ul class="school-list clearfix">
          <li class="school-item" v-for="(item,index) in findAllSchoolList" :key="index">
            <div class="flex item-detail">
                <div class="item-img">
                    <img :src="item.schoolLogo?item.schoolLogo:'/static/logo.png'" alt="校区图片">
                </div>
              <div class="flex-1">
                <div class="item-title">{{item.schoolName}}</div>
                <div class="item-phone">联系电话：{{item.schoolPhone}}</div>
                <div class="item-address">地址：{{item.schoolAddress}}</div>
              </div>
            </div>
            <div class="item-operate">
              <el-button type="primary" plain size="mini" @click="toEdit(item.id)">编辑</el-button>
              <el-button type="info" plain size="mini" @click="deleteItem(index)">删除</el-button>
            </div>
          </li>
        </ul>
    </div>
    <!-- 空数据 -->
    <div class="plr36" v-else>
        <div class="empty-list flex-vc">
            <div><i class="iconfont">&#xe680;</i></div>
            <div class="martop15">您还没有设置校区， <a href="javascript:;" @click="toEdit(0)">点击这里</a>新增校区吧</div>
        </div>
    </div>
  </div>
</template>
<script>
import common from "@/assets/js/common.js";
export default {
  data() {
    return {
      findAllSchoolList: [],
      newPath:'',
    };
  },
  methods: {
    loadData() {
        let that = this
        common.ajax({
            'url': eduAPI.activeAPI.findAllSchoolList,
            'type': "GET",
            'success': function(data) {
                console.log(data);
                if (data.data) {
                    for(let i = 0; i < data.data.length;i++){
                        if(data.data[i].schoolLogo){
                            data.data[i].schoolLogo = that.newPath + data.data[i].schoolLogo
                        }
                    }
                    that.findAllSchoolList = data.data;
                }
            }
        });
    },
    toEdit(id) {
        let that = this;
        that.$router.push({
            name: "communitySettingEdit",
            params: { schoolId: id }
        });
    },
    // 删除
    deleteItem(index) {
        let that = this;
        parent.window.postMessage("openMask()", "*");
        that.$confirm("是否删除该校区?", "校区删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            var params = {};
            common.ajax({
                'url':eduAPI.activeAPI.delSchool,
                'type': "POST",
                'data':{
                    schoolId: that.findAllSchoolList[index].id
                },
                'success': function(data) {
                    that.findAllSchoolList.splice(index, 1);
                    parent.window.postMessage("closeMask()", "*");
                }
            })
        })
        .catch(() => {
          parent.window.postMessage("closeMask()", "*");
          that.$message({
            showClose: true,
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  mounted() {
      let that = this
      //素材库和域名调用接口
        common.ajax({
            'url': eduAPI.activeAPI.getMaterialUrl,
            'type': "GET",
            'success': function(data) {
                console.log(data,'素材库和域名调用接口')
                if(data.data){
                    that.newPath = data.data.materialImageUrl
                    that.loadData();
                }
            }
        })
    
    
  }
};
</script>
